package com.technology.education.ui.student.collection

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.toDslItemList
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.CollectCourse
import com.technology.education.ext.loadImage
import com.technology.education.ext.setPageItems
import com.technology.education.ui.student.course_detail.CourseDetailActivity
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.startActivity

/**
 *@author:create by BZY
 *Date: 2022/3/11 07
 * 我的收藏
 */
class CollectionFragment : BaseRecyclerTopBarFragment<CollectionViewModel>() {
    override fun initViews() {
        setTitle("我的收藏")
        initGrid(3)
        initLoadMore()
    }

    override fun lazyLoadData() {
        loading()
        mViewModel.getData()
    }

    private fun setUI(list: List<CollectCourse>) {
        renderAdapter {
            setPageItems(list.toDslItemList(R.layout.item_course_collectioin) {
                itemSpanCount = 1
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemData?.let {
                        if (it is CollectCourse) {
                            itemHolder.tv(R.id.tv_name)?.text = it.name
                            itemHolder.v<ShapeableImageView>(R.id.iv_conver)?.apply {
                                loadImage(it.url, img = this)
                            }
                            itemHolder.clickItem { _ ->
                                startActivity(
                                    CourseDetailActivity::class,
                                    Pair("id", it.courseId ?: ""),
                                    Pair("title", it.name),
                                    Pair("url", it.url ?: "")
                                )
                            }
                        }
                    }

                }
            }, mViewModel.pageIndex)
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { result ->
                hasMore(result.total)
                setUI(result.list ?: listOf())
            })
        }
    }
}